package com.innovation.sys.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.innovation.sys.pojo.SSysLog;

public class ExportExcelUtils {

	private static Logger logger = LoggerFactory.getLogger(ExportExcelUtils.class);

	public static Workbook readExcel(String filePath) {
		InputStream in = null;
		Workbook work = null;
		try {
			in = new FileInputStream(filePath);
			// 读取工作簿
			XSSFWorkbook wordBook = new XSSFWorkbook(in);
			// 读取第一张工作表,从0开始
			XSSFSheet sheet = wordBook.getSheetAt(0);
			// 读取行,此处应该使用循环
			XSSFRow row = sheet.getRow(0);
			// 读取单元格
			XSSFCell cell = row.getCell(0);
			// 获取单元格对象
			String value = cell.getStringCellValue();
			// 此处进行单元格数据截取拼接，将其放入list中
			logger.info("sheet中的数据为:{}" + value);
			// 关闭输入流
			in.close();
			// 关闭工作簿
			wordBook.close();
		} catch (FileNotFoundException e) {
			logger.info("error:{}" ,e);
		} catch (IOException e) {
			logger.info("error:{}" ,e);
		}
		return work;
	}

	public static void writeExcel(List<SSysLog> data, String fileName) throws IOException {
		File existFile = FileUtils.createFile(fileName);
		FileOutputStream outputStream = null;
		try {
			// 创建工作簿
			XSSFWorkbook workbook = new XSSFWorkbook();
			// 创建工作表
			XSSFSheet sheet = workbook.createSheet();
			// 创建带样式的表头
			setCellStyle(workbook, sheet);
			int rows = 1;
			for (SSysLog logs : data) {
				// 创建行
				XSSFRow row = sheet.createRow(rows);
				// 创建单元格
				XSSFCell cell = row.createCell(0, Cell.CELL_TYPE_STRING);
				cell.setCellValue(logs.getLogId());
				XSSFCell cell1 = row.createCell(1, Cell.CELL_TYPE_STRING);
				cell1.setCellValue(logs.getLogUser());
				XSSFCell cell2 = row.createCell(2, Cell.CELL_TYPE_STRING);
				cell2.setCellValue(logs.getLogDesc());
				XSSFCell cell3 = row.createCell(3, Cell.CELL_TYPE_STRING);
				cell3.setCellValue(LangUtils.formatDate(logs.getCreateTime(), "yyyyMMdd HH:mm:ss"));
				rows++;
			}
			outputStream = new FileOutputStream(existFile);
			workbook.write(outputStream);
			// 关闭工作簿
			workbook.close();
		} catch (IOException e) {
			logger.info("error:{}" ,e);
		} finally {
			outputStream.close();
		}
	}

	private static void setCellStyle(XSSFWorkbook wb, XSSFSheet sheet) {
		XSSFCellStyle cellStyle = wb.createCellStyle();
		// 设置背景
		cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
		cellStyle .setFillPattern(FillPatternType.SOLID_FOREGROUND);
		// 设置表头，创建行
		XSSFRow row0 = sheet.createRow(0);
		// 创建单元格
		XSSFCell cell0 = row0.createCell(0, Cell.CELL_TYPE_STRING);
		cell0.setCellValue("序号");
		XSSFCell cell00 = row0.createCell(1, Cell.CELL_TYPE_STRING);
		cell00.setCellValue("操作人");
		XSSFCell cell000 = row0.createCell(2, Cell.CELL_TYPE_STRING);
		cell000.setCellValue("操作描述");
		XSSFCell cell0000 = row0.createCell(3, Cell.CELL_TYPE_STRING);
		cell0000.setCellValue("操作时间");
		// 设置单元格样式
		cell0.setCellStyle(cellStyle);
		cell00.setCellStyle(cellStyle);
		cell000.setCellStyle(cellStyle);
		cell0000.setCellStyle(cellStyle);
	}

}
